package org.example.consumer.controller;

import org.example.api.UserApi;
import org.example.common.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {
    @Resource
    private UserApi userApi;

    @GetMapping("/getUserByName")
    public String getUserByName(@RequestParam("username") String username){
        User user = userApi.getUserByName(username);
        return user != null ? user.getName():"null";
    }

    @GetMapping("/addUser")
    public Map<String,Object> addUser(@RequestBody User user){
        Map<String,Object> map = new HashMap(2);
        map.put("code",500);
        map.put("msg","error！");
        if (user == null){
            return map;
        }
        if (userApi.addUser(user)>0){
            map.put("code",0);
            map.put("msg","新增成功！");
        }

        return map;
    }
}
